"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysqlClient = void 0;
const mysql = require("mysql2");
const Logger_1 = require("./Logger");
class MysqlClient {
    static create(database, host, port, user, pass) {
        return __awaiter(this, void 0, void 0, function* () {
            let mysqlClient = new MysqlClient();
            mysqlClient.database = database;
            mysqlClient.host = host;
            mysqlClient.port = port;
            mysqlClient.user = user;
            mysqlClient.pass = pass;
            mysqlClient.pool = mysql.createPool({
                connectionLimit: 10,
                host: mysqlClient.host,
                port: mysqlClient.port,
                user: mysqlClient.user,
                password: mysqlClient.pass,
                database: mysqlClient.database
            });
            // Logger.info("MysqlClient.init", database, host, port, user, pass)
            mysqlClient.init();
            return mysqlClient;
        });
    }
    init() {
        this.pool.on('release', (connection) => {
            // Logger.log('MysqlClient released', this.database, connection.threadId);
        });
        return new Promise((resolve, reject) => {
            this.pool.on('connection', (connection) => {
                // Logger.log("MysqlClient.connection=", this.database, connection)
                resolve(null);
            });
        });
    }
    test() {
        Logger_1.Logger.log("test");
        // this.select("SELECT * FROM users;", () => {
        // })
    }
    queryPromise(sql, sqlParams = null) {
        return new Promise((resolve, reject) => {
            this.query(sql, sqlParams, (err, results, fields) => {
                resolve(results);
            });
        });
    }
    query(sql, sqlParams = null, callback = null) {
        if (sqlParams && sqlParams.length > 0) {
            for (let i = 0; i < sqlParams.length; i++) {
                // sqlParams[i] = encodeURIComponent(sqlParams[i])
                // sqlParams[i] = escape(sqlParams[i])
            }
        }
        this.pool.getConnection((err, connection) => {
            if (err) {
                Logger_1.Logger.warn("query error", err);
                Logger_1.Logger.log("query error", err);
                callback && callback(err, [], []);
                return;
            }
            // Use the connection
            connection.query(sql, sqlParams, (error, results, fields) => {
                // When done with the connection, release it.
                connection.release();
                // Handle error after the release.
                if (error) {
                    Logger_1.Logger.log("query error=", sql, sqlParams, this.database, error);
                    Logger_1.Logger.warn("query error=", sql, sqlParams, this.database, error);
                    callback && callback(error, [], []);
                    return;
                }
                // Don't use the connection here, it has been returned to the pool.
                // Logger.log("result", results)
                // Logger.log("fields", fields)
                callback && callback(err, results, fields);
            });
        });
    }
    destroy() {
        Logger_1.Logger.info("MysqlClient.destroy", this.database);
        this.pool.end((err) => {
            Logger_1.Logger.info("MysqlClient.end=", err);
            this.pool.removeAllListeners();
            this.pool = null;
        });
    }
}
exports.MysqlClient = MysqlClient;
